package com.google.code.whitetask.old;

public class IfRunDecorator extends AbstractFluentTask {
  private Task task;
  private TaskResultType expectedTaskResult;
  private Task taskToRun;

  public IfRunDecorator(Task task) {
    this.task = task;
  }

  public IfRunDecorator ifResultIs(TaskResultType expectedTaskResult) {
    this.expectedTaskResult = expectedTaskResult;
    return this;
  }

  public IfRunDecorator thenRuns(Task task) {
    if (expectedTaskResult == null) {
      throw new MethodCallNotSatisfiedPrerequisitesException(
          "You should set an expected result before calling this method.");
    }
    this.taskToRun = task;
    return this;
  }

  public TaskResultType execute(TaskContext context) {
    TaskResultType result = task.execute(context);
    return result == expectedTaskResult ? taskToRun.execute(context) : result;
  }
}
